package com.wsh.sys.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsh.base.BaseVo;
import com.wsh.dto.sys.DepartmentDto;
import com.wsh.entity.sys.DepartmentEntity;
import com.wsh.mapper.sys.DepartmentDao;
import com.wsh.utils.PageUtils;
import com.wsh.vo.sys.DepartmentTree;
import com.wsh.vo.sys.DepartmentVo;
import com.wsh.sys.DepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author 王思涵
* @date 2020-03-14
*/
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentDao, DepartmentEntity> implements DepartmentService {

    @Override
    public IPage<DepartmentEntity> getList(DepartmentDto departmentDto) {
        // 分页数据处理
        IPage<DepartmentEntity> pages = PageUtils.getPage(departmentDto);

        // 分页查询处理
        pages.setRecords(baseMapper.getDeparmentTable("0"));

        return pages;
    }

    @Override
    public BaseVo<DepartmentVo> queryVo(String formId) {
        DepartmentVo departmentvo = new DepartmentVo();

        DepartmentEntity departmentEntity = getById(formId);

        BeanUtils.copyProperties(departmentEntity, departmentvo);

        return new BaseVo<>(departmentvo);
    }

    @Override
    public boolean saveDepartment(DepartmentDto departmentDto) {
        DepartmentEntity departmentEntity = new DepartmentEntity();

        BeanUtils.copyProperties(departmentDto, departmentEntity);

        departmentEntity.setCreateTime(new Date());

        boolean saveEntity = saveOrUpdate(departmentEntity);

        return saveEntity ;
    }

    @Override
    public List<DepartmentTree> departTree(String parentId) {
        return baseMapper.departTree(parentId);
    }
}
